package com.base.controller;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.base.comm.CommContent;
import com.base.po.voip.News;
import com.base.service.LogsService;
import com.base.service.NewsService;
import com.base.utils.IP;

/**
 * 新闻操作
 * 
 * @author K
 *
 */
@Controller
@RequestMapping("/admin")
public class content_xinwencaozuo {
	@Autowired
	private NewsService newsService;
	@Autowired
	private LogsService logsService;// log日志
	
	// 用户首页
	@RequestMapping("/content_xinwencaozuo")
	public ModelAndView index(HttpServletRequest request,String id) throws Exception {
		// 返回ModelAndView
		ModelAndView modelAndView = new ModelAndView();
		if (request.getSession().getAttribute("adminUserId") == null) {
			modelAndView.setViewName("jsp/login");
			return modelAndView;
		}
		
		if (id == null) {
			modelAndView.setViewName("jsp/datanotfound");
			return modelAndView;
		}
		
		News news = newsService.selectByPrimaryKey(id);
		if(news != null) {
			news.setDatetime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").
					format(news.getDate()));
			switch (news.getCategory()) {
			case 1:
				news.setType("社会");
				break;
			case 2:
				news.setType("国内");
				break;
			case 3:
				news.setType("国际");
				break;
			case 4:
				news.setType("娱乐");
				break;
			case 5:
				news.setType("体育");
				break;
			case 6:
				news.setType("军事");
				break;
			case 7:
				news.setType("科技");
				break;
			case 8:
				news.setType("财经");
				break;
			case 9:
				news.setType("时尚");
				break;
			case 10:
				news.setType("游戏");
				break;
			case 11:
				news.setType("汽车");
				break;
			case 12:
				news.setType("笑话");
				break;
			case 13:
				news.setType("健康");
				break;
			default:
				break;
			}
			request.setAttribute("news", news);
			// 设置页面进行跳转
			modelAndView.setViewName("jsp/content_xinwenxiangqing");
			return modelAndView;
		}
		
		modelAndView.setViewName("jsp/datanotfound");
		return modelAndView;
	}
	
	String SUCCESS = "success";
	@RequestMapping(value = "/updateNews", method = RequestMethod.POST)
	public void updateCompanyNews(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(value = "id", required = true) String id,
			@RequestParam(value = "datastatus", required = true)Integer datastatus) throws Exception {
		// 返回参数
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=UTF-8");
//		String parent = (String) request.getSession().getAttribute("adminUserId");
		
        News news = newsService.selectByPrimaryKey(id);
        
        System.out.println(datastatus);
		PrintWriter out = response.getWriter();
			if (id == null) {
				out.print("浏览器在点击的过程中发现丢失了用户信息，请您刷新重试！");
				return;
			} else {
				int code = newsService.updateDataStatusByID(id, datastatus);
			
				String usernamesys = (String) request.getSession().getAttribute("useracc");
                 
				if (code == 1) {
					logsService.insert(CommContent.TYPE_DELET, "用户：" + usernamesys + "删除成功标题：“"+news.getTitle()
					+"”的新闻", usernamesys,true, IP.getIpAddr(request));
					out.print(SUCCESS);
				} else {
					logsService.insert(CommContent.TYPE_DELET, "用户：" + usernamesys + "删除失败标题：“"+news.getTitle() 
							+"”的新闻", usernamesys,true, IP.getIpAddr(request));
					out.print("更改失败，请您重试");
				}
		}
	}
}
